// 加载备品统计数据
function loadSparePartsData(startDate, endDate) {
    const chart = echarts.getInstanceByDom(document.getElementById('spare-parts-chart'));

    $.ajax({
        url: '/stockOperation/sparePartsStats',
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({
            startTime: startDate,
            endTime: endDate
        }),
        success: function(response) {
            if (response.code === 0 && response.data) {
                const data = response.data;
                updateSparePartsChart(chart, data);
            } else {
                layer.msg('获取备品统计数据失败: ' + response.msg);
            }
        },
        error: function(xhr, status, error) {
            layer.msg('获取备品统计数据出错');
            console.error('获取备品统计数据出错:', error);
        }
    });
}

// 更新备品统计图表
function updateSparePartsChart(chart, data) {
    const inStats = data.inStats || {};
    const outStats = data.outStats || {};

    // 获取所有物料类型
    const categories = Array.from(new Set([
        ...Object.keys(inStats),
        ...Object.keys(outStats)
    ]));

    // 准备图表数据
    const inData = categories.map(cat => inStats[cat] || 0);
    const outData = categories.map(cat => outStats[cat] || 0);

    const option = {
        backgroundColor: "#323a5e",
        tooltip: {
            trigger: "axis",
            axisPointer: {
                type: "shadow"
            }
        },
        grid: {
            left: "2%",
            right: "4%",
            bottom: "14%",
            top: "16%",
            containLabel: true
        },
        legend: {
            data: ["入库", "出库"],
            right: 10,
            top: 12,
            textStyle: {
                color: "#fff"
            },
            itemWidth: 12,
            itemHeight: 10
        },
        xAxis: {
            type: "category",
            data: categories,
            axisLine: {
                lineStyle: {
                    color: "white"
                }
            },
            axisLabel: {
                textStyle: {
                    fontFamily: "Microsoft YaHei",
                    color: "#fff"
                }
            }
        },
        yAxis: {
            type: "value",
            axisLine: {
                show: false,
                lineStyle: {
                    color: "white"
                }
            },
            splitLine: {
                show: true,
                lineStyle: {
                    color: "rgba(255,255,255,0.3)"
                }
            },
            axisLabel: {
                color: "#fff"
            }
        },
        dataZoom: [
            {
                show: true,
                height: 12,
                xAxisIndex: [0],
                bottom: "8%",
                start: 0,
                end: 50,
                handleIcon: "path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z",
                handleSize: "110%",
                handleStyle: {
                    color: "#d3dee5"
                },
                textStyle: {
                    color: "#fff"
                },
                borderColor: "#90979c"
            },
            {
                type: "inside",
                show: true,
                start: 0,
                end: 50
            }
        ],
        series: [
            {
                name: "入库",
                type: "bar",
                barWidth: "15%",
                itemStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            { offset: 0, color: "#248ff7" },
                            { offset: 1, color: "#6851f1" }
                        ]),
                        barBorderRadius: 11
                    }
                },
                data: inData
            },
            {
                name: "出库",
                type: "bar",
                barWidth: "15%",
                itemStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            { offset: 0, color: "#8bd46e" },
                            { offset: 1, color: "#09bcb7" }
                        ]),
                        barBorderRadius: 11
                    }
                },
                data: outData
            }
        ]
    };

    chart.setOption(option);
}

// 辅助函数：格式化日期为YYYY-MM-DD
function formatDate(date) {
    const d = new Date(date);
    let month = '' + (d.getMonth() + 1);
    let day = '' + d.getDate();
    const year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}